// pages/buy/index.js
import {
    axios
} from "../service/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cinemaExtendList: [], //放的是每个电影院对应的最低价
        showCinemas: [], //对应的时间 有该电影院有该电影上映
        ellipsis: false,
        cinemas: [], //显示的影院的集合
        checkDate:0,//选择的时间
    },
    gotoDetail(e){
        let  cinemaId = e.currentTarget.dataset.id;
        let  date  = this.data.checkDate;
        //console.log(cinemaId);
        wx.navigateTo({
          url: '/pages/cinemaDetail/index?cinemaId='+cinemaId +"&date="+date
        })

    },
  async  change(info) {
         let  cinemaIds=   this.data.showCinemas[info.detail.index].cinemaList;
         //集合
         let res2 = await axios({
            method: "post",
            url: "https://m.maizuo.com/gateway?k=7063907",
            data: {
                cityId: 110100,
                cinemaIds: cinemaIds.join(",")
            },
            header: {
                "X-Host": "mall.film-ticket.cinema.batch-cinema"
            }
        })
       // console.log(info);
        this.setData({
            checkDate:this.data.showCinemas[info.detail.index].date,
            cinemas: res2.data.data.cinemas.map(item => {
                item.lowPrice = (item.lowPrice / 100).toFixed(2);
                return item;
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        let {
            filmid: filmId
        } = options;
        let res = await axios({
            method: "get",
            url: "https://m.maizuo.com/gateway/",
            data: {
                filmId,
                cityId: 110100,
                k: 2860233
            },
            header: {
                "X-Host": "mall.film-ticket.cinema.film-show-cinema"
            }
        })
        let weekList = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
        let tempDate = new Date(); //系统的当前时间
        let today = (tempDate.getMonth() + 1) + "月" + tempDate.getDate() + "日";

        let tempDate1 = new Date(); //系统的当前的明天
        tempDate1.setDate(tempDate1.getDate() + 1);
        let tomorrow = (tempDate1.getMonth() + 1) + "月" + tempDate1.getDate() + "日";

        let tempDate2 = new Date(); //系统的当前的后台
        tempDate2.setDate(tempDate2.getDate() + 2);
        let tomorrow2 = (tempDate2.getMonth() + 1) + "月" + tempDate2.getDate() + "日";

        let dateInfo = {
            [today]: "今天",
            [tomorrow]: "明天",
            [tomorrow2]: "后天"
        }
        //默认还需要显示第一天时间的影院
        res.data.data.showCinemas.sort((a,b)=>a.showDate-b.showDate);//根据时间排序
        let cinemaIds = res.data.data.showCinemas[0]["cinemaList"];//默认的影院
        let  checkDate  = res.data.data.showCinemas[0]["showDate"];//默认时间
        //再去调用另一个接口
        let res2 = await axios({
            method: "post",
            url: "https://m.maizuo.com/gateway?k=7063907",
            data: {
                cityId: 110100,
                cinemaIds: cinemaIds.join(",")
            },
            header: {
                "X-Host": "mall.film-ticket.cinema.batch-cinema"
            }
        })
        console.log(res2);



        let showCinemas = res.data.data.showCinemas.map(item => {
            let date = new Date(item.showDate * 1000);
            let day = date.getDay(); //表示星期几
            let str = (date.getMonth() + 1) + "月" + date.getDate() + "日";
            item.date = item.showDate;//如果是引用类型 就会出问题
            item.showDate = dateInfo[str] ? dateInfo[str] + str : weekList[day] + str;

            return item;
        });
        let cinemaExtendList = res.data.data.cinemaExtendList
        this.setData({
            cinemaExtendList,
            showCinemas,
            checkDate,
            cinemas: res2.data.data.cinemas.map(item => {
                item.lowPrice = (item.lowPrice / 100).toFixed(2);
                return item;
            })
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})